<template>
    <el-dialog
            :title="!dataForm.visualizationId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="可视化ID" prop="visualizationId">
                        <el-input v-model.number="dataForm.visualizationId" placeholder="可视化ID"></el-input>
                </el-form-item>
                <el-form-item label="仪表盘名称" prop="dashboardName">
                        <el-input v-model="dataForm.dashboardName" placeholder="仪表盘名称"></el-input>
                </el-form-item>
                <el-form-item label="数据报表类型（日、周、月）" prop="reportType">
                        <el-input v-model="dataForm.reportType" placeholder="数据报表类型（日、周、月）"></el-input>
                </el-form-item>
                <el-form-item label="数据导出格式（CSV、Excel等）" prop="exportFormat">
                        <el-input v-model="dataForm.exportFormat" placeholder="数据导出格式（CSV、Excel等）"></el-input>
                </el-form-item>
                <el-form-item label="实时数据展示内容" prop="realTimeData">
                        <el-input v-model="dataForm.realTimeData" placeholder="实时数据展示内容"></el-input>
                </el-form-item>
                <el-form-item label="关键指标展示内容" prop="keyMetrics">
                        <el-input v-model="dataForm.keyMetrics" placeholder="关键指标展示内容"></el-input>
                </el-form-item>
                <el-form-item label="数据报表内容" prop="reportContent">
                        <el-input v-model="dataForm.reportContent" placeholder="数据报表内容"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            visualizationId: null,
            dashboardName: null,
            reportType: null,
            exportFormat: null,
            realTimeData: null,
            keyMetrics: null,
            reportContent: null
          },
          dataRule: {
            visualizationId: [
                        { required: true, message: '可视化ID不能为空', trigger: 'blur' }
            ],
            dashboardName: [
                        { required: true, message: '仪表盘名称不能为空', trigger: 'blur' }
            ],
            reportType: [
                        { required: true, message: '数据报表类型（日、周、月）不能为空', trigger: 'blur' }
            ],
            exportFormat: [
                        { required: true, message: '数据导出格式（CSV、Excel等）不能为空', trigger: 'blur' }
            ],
            realTimeData: [
                        { required: true, message: '实时数据展示内容不能为空', trigger: 'blur' }
            ],
            keyMetrics: [
                        { required: true, message: '关键指标展示内容不能为空', trigger: 'blur' }
            ],
            reportContent: [
                        { required: true, message: '数据报表内容不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.visualizationId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.visualizationId = id || 0
            if (this.dataForm.visualizationId) {
              this.$http({
                url: this.$http.adornUrl(`/app/data_visualization/info/${this.dataForm.visualizationId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appdatavisualization
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/data_visualization/${!this.dataForm.visualizationId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>